package cn.lysj.framework.foodv2.controller;

import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.CommonConstant;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageUtil;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.relation.RelationUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.validate.annotation.Validator;
import cn.lysj.framework.foodv2.domain.Food;
import cn.lysj.framework.foodv2.dto.FoodListDto;
import cn.lysj.framework.foodv2.service.FoodService;
import cn.lysj.framework.foodv2.vo.FoodDto;
import cn.lysj.framework.foodv2.vo.FoodSaleVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author drj
 * @description 支付宝用户控制层
 * @create 2018-11-09 11:20:17
 **/
@Slf4j
@RestController
@Api(description = "支付宝用户管理接口")
@RequestMapping("/food")
public class FoodController extends BaseController<Food> {

    @Resource
    private FoodService foodService;

    @Override
    public FoodService getService() {
        return foodService;
    }

    @Override
    public Resp save(Food food) {
        return null;
    }

    @Override
    public Resp list(Food food, PageVo pageVo) {
        return null;
    }

    @Override
    public Resp update(Food food) {
        return null;
    }

    @Override
    public Resp delete(String[] ids) {
        return foodService.deleteRewrite(ids);
    }

    @PostMapping("/update_rewrite")
    public Resp update(@Validator FoodDto foodDto) {
        return foodService.update(foodDto);
    }

    @PostMapping("/save_rewrite")
    public Resp save(@Validator FoodDto foodDto) {
        if (ParamUtil.isBlank(foodDto.getLogo())) {
            return new Resp(Resp.PARAM_ERROR, "请上传图片!");
        }

        return foodService.save(foodDto);
    }

    @GetMapping("/list_rewrite")
    public Resp list(@CurrentUser User user, FoodListDto foodListDto, PageVo pageVo) {
        return foodService.list(user, foodListDto, pageVo);
    }

    @GetMapping("/show_act_sell_num")
    public Resp showActSellNum(@CurrentUser User user) {
        Map<String, Object> map = new HashMap<>();

        List<FoodSaleVo> list = foodService.getRepository().findFoodSale(user.getStoreId());
        String[] nameData = new String[list.size()]; //名称数组数据
        Integer[] salesData = new Integer[list.size()];//销量数组数据
        for (int i = 0; i < list.size(); i++) {
            nameData[i] = list.get(i).getName();
            salesData[i] = list.get(i).getActSellNum();
            map.put("maxSale",salesData[0]);
        }
        map.put("nameData", nameData);
        map.put("salesData", salesData);
        return new Resp(map);
    }

    @PostMapping("/change_is_use")
    public Resp changeIsUse(Integer isUse,String[] ids){
        List<Food> list = getService().findAll(ids);
        for (Food e : list) {
            if(e==null){
                continue;
            }
            e.setIsUse(isUse);
        }
        getService().save(list);
        return new Resp("修改成功!");
    }


}
